package com.taikang.system.controller.front;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.taikang.base.controller.BaseController;
import com.taikang.system.entity.SysEmailServerConfig;
import com.taikang.system.entity.SysEmailTemplate;
import com.taikang.system.form.AdminEmailForm;
import com.taikang.system.service.ISysEmailServerConfigService;
import com.taikang.system.service.ISysEmailTemplateService;

/**
 * 邮件服务管理
 * 
 * @author taikang
 * 
 */
@Controller("frontEmailController")
@Scope("prototype")
@RequestMapping(value = "/front/email")
public class FrontEmailController extends BaseController  {

	@Autowired
	private ISysEmailServerConfigService emailServerConfigService;
	@Autowired
	private ISysEmailTemplateService emailTemplateService;
	
	@Value("${domain.path}")
	private String dmPath;

	/**
	 * 邮件服务器配置
	 * 
	 * @return
	 */
	@RequestMapping(value = "/config.htm", method = RequestMethod.GET)
	public ModelAndView config(@ModelAttribute AdminEmailForm model,HttpServletRequest request) {
		ModelAndView view = new ModelAndView();
		view.setViewName("/admin/email/config");
		Map<String,Object> data = view.getModel();
		SysEmailServerConfig emailServerConfig = emailServerConfigService.getEmailServerConfig();
		data.put("serverConfig", emailServerConfig);
		return view;
	}

	/**
	 * 修改邮件服务器配置
	 * 
	 * @return
	 */
	@RequestMapping(value = "/saveConfig.htm", method = RequestMethod.POST)
	public ModelAndView saveConfig(@ModelAttribute AdminEmailForm model,HttpServletRequest request) {
		try {
			SysEmailServerConfig emailServerConfig = model.getServerConfig();
			if (null == emailServerConfig) {
				return ajaxJSON(Status.error, "保存失败");
			}
			emailServerConfigService.update(emailServerConfig);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ajaxJSON(Status.success, "保存成功");
	}

	/**
	 * 邮件模板列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "/tempList.htm", method = RequestMethod.GET)
	public ModelAndView tempList() {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("/admin/email/tempList");
		return modelAndView;
	}
	
	/**
	 * 邮件模板列表
	 * 
	 * @return
	 */
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/ajaxTempList.htm", method = RequestMethod.POST)
	//@Action(value = "ajaxTempList")
	public void ajaxTempList(HttpServletResponse response) {
		List<SysEmailTemplate> templateList = emailTemplateService.getAllEmailTemplates();
		JSONObject root = new JSONObject();
		JSONArray array = new JSONArray();
		root.put("resultList", array);
		for(int i=0;i<templateList.size();i++){
			JSONObject json = new JSONObject();
			SysEmailTemplate obj = templateList.get(i);
			json.put("type", obj.getType());
			json.put("content", obj.getContent());
			json.put("id", obj.getId());
			array.add(json);
		}
		ajaxPageResult(root,response);
	}

	/**
	 * 添加或修改模板模板
	 * 
	 * @return
	 */
	@RequestMapping(value = "/viewTemplate.htm", method = RequestMethod.GET)
	//@Action(value = "viewTemplate", results = { @Result(name = SUCCESS, location = "/WEB-INF/pages/admin/email/viewTemplate.jsp") })
	public ModelAndView viewTemplate(@ModelAttribute AdminEmailForm model,HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("/admin/email/viewTemplate");
		
		String id = model.getId();
		if (StringUtils.isBlank(id)) {
			modelAndView.setViewName(ERROR_PAGE);
			return modelAndView;
		}
		SysEmailTemplate emailTemplate = emailTemplateService
				.getEmailTemplateById(id);
		if (null == emailTemplate) {
			modelAndView.setViewName(ERROR_PAGE);
			return modelAndView;
		}
		Map<String,Object> data = modelAndView.getModel();
		data.put("emailTemplate", emailTemplate);
		return modelAndView;
	}

	/**
	 * 添加或修改模板模板
	 * 
	 * @return
	 */
	@RequestMapping(value = "/saveOrUpdateTemplate.htm", method = RequestMethod.POST)
	public ModelAndView saveOrUpdateTemplate(@ModelAttribute AdminEmailForm model,HttpServletRequest request) {
		SysEmailTemplate emailTemplate = model.getEmailTemplate();
		if (null == emailTemplate) {
			return ajaxJSON(Status.error, "保存失败");
		}
		emailTemplateService.updateEmailTemplate(emailTemplate);
		return ajaxJSON(Status.success, "保存成功");
	}

}
